<template>
  <div>
    <!-- 使用isAdd 展示添加还是编辑 -->
    <el-dialog :title="info.isAdd?'分类添加':'分类编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <!-- 2.绑定数据 -->
        <el-form-item label="上级分类" label-width="120px">
          <el-select placeholder="请选择" v-model="form.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <!-- 3.图片 -->
          <!-- 我的上传文件 -->
          <!-- <div class="my-upload">
            <h3 class="my-plus">+</h3>
            <img v-if="imgUrl" class="my-img" :src="imgUrl" alt />
            <input type="file" @change="changeFile" />
          </div> -->

          <!-- element-ui上传文件 -->
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
           :on-change="changeFile2"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { reqcateadd, reqcateedit, reqcateinfo, reqroleedit, reqroleinfo } from "../../../request/api";
import { errorAlert, successAlert } from "../../../utils/alert";
import path from "path";
import {mapGetters,mapActions} from "vuex"
export default {
  components: {},
  props: ["info"],
  data() {
    return {
      // 1.初始化数据
      form: {
        pid: "",
        catename: "",
        img: null, //File
        status: 2,
      },
      //图片的地址
      imgUrl: "",
    };
  },
  computed:{
    ...mapGetters({
      list:"cate/list"
    })
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqList:"cate/reqList"
    }),
    //修改图
    changeFile(e) {
      //得到文件
      let file = e.target.files[0];

      // 判断大小 1M=1024kb 1kb=1024b
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }

      // 判断文件类型
      let imgArr = [".jpg", ".jpeg", ".png", ".gif"];

      /*let name=file.name ;// "1.1.1.1.1.jpg"
      let extname=name.slice(name.lastIndexOf("."));*/

      let extname = path.extname(file.name);
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }

      //根据文件生成一个图片地址
      this.imgUrl = URL.createObjectURL(file);

      //给form.img
      this.form.img = file;
    },

    //修改图片
    changeFile2(e){
      let file=e.raw;

      //根据文件生成一个图片地址
      this.imgUrl = URL.createObjectURL(file);

      //给form.img
      this.form.img = file;

    },

    //取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //重置form
    empty() {
      this.form = {
        pid: "",
        catename: "",
        img: null, //File
        status: 2,
      };
      this.imgUrl = "";
    },
    //添加
    add() {
      // 数据交互
      reqcateadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.reqList()
        }
      });
    },
    //修改了上级菜单
    changePid() {
      // 如果上级菜单是顶级菜单，就是目录；否则就是菜单
      if (this.form.pid == 0) {
        this.form.type = 1;
      } else {
        this.form.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form=res.data.list;
          this.form.id=id;
          //给图片赋值
          this.imgUrl=this.$pre+this.form.img
        }
      });
    },
    //修改
    update() {    
      reqcateedit(this.form).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
         this.reqList()
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @header-color;
  position: relative;
}
.my-plus {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 100px;
  color: @primary;
}
.my-img {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.my-upload input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}

/deep/.avatar-uploader .el-upload { 
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>